package com.sunwayworld.basemodule.business.prefolder.validators;

import com.sunwayworld.basemodule.business.batch.bean.LimsBatchBean;
import com.sunwayworld.basemodule.business.batch.service.LimsBatchSampleService;
import com.sunwayworld.basemodule.business.operationbatch.bean.LimsOperationBatchBean;
import com.sunwayworld.basemodule.business.operationbatch.bean.LimsPreTaskSupplementBean;
import com.sunwayworld.basemodule.business.operationbatch.service.LimsPreTaskSupplementService;
import com.sunwayworld.basemodule.business.prefolder.bean.LimsPreFolderBean;
import com.sunwayworld.basemodule.business.prefolder.service.LimsPreFolderService;
import com.sunwayworld.basemodule.samplemanage.sample.bean.LimsSampleBean;
import com.sunwayworld.framework.i18n.I18nHelper;
import com.sunwayworld.framework.mybatis.mapper.MapperParameter;
import com.sunwayworld.framework.mybatis.mapper.MatchPattern;
import com.sunwayworld.framework.mybatis.mapper.SearchFilter;
import com.sunwayworld.framework.restful.data.RestJsonWrapperBean;
import com.sunwayworld.framework.spring.annotation.GikamBean;
import com.sunwayworld.framework.utils.ArrayUtils;
import com.sunwayworld.framework.utils.StringUtils;
import com.sunwayworld.framework.validator.data.DataValidator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

@Repository
@GikamBean
public class LimsOperationBatchSubmitValidator implements DataValidator {

    @Autowired
    private LimsPreFolderService preFolderService;

    @Autowired
    private LimsPreTaskSupplementService preTaskSupplementService;

    @Override
    public boolean doValid(Object... args) {
        RestJsonWrapperBean wrapper = (RestJsonWrapperBean) args[0];
        List<LimsOperationBatchBean> operationBatchBeans = wrapper.parse(LimsOperationBatchBean.class);
        List<String> operationBatchs = operationBatchBeans.stream().map(LimsOperationBatchBean::getOperationBatch).collect(Collectors.toList());

        List<LimsPreFolderBean> preFolderBeans = preFolderService.getDao().selectListByOneColumnValues(operationBatchs, "operationBatch", ArrayUtils.asList("id","operationBatch","status"))
                .stream().filter(p -> StringUtils.equals(p.getStatus(), "draft")).collect(Collectors.toList());
        if (operationBatchs.stream().anyMatch(b -> preFolderBeans.stream().noneMatch(p -> StringUtils.equals(p.getOperationBatch(), b)))) {
            this.addConstraintViolation(I18nHelper.getMessage("OPERATION_BATCH.SUBBMIT.CHECK_SAMPLE_NOTEMPTY"));
            return false;
        }
        List<Long> preFolderIds = preFolderBeans.stream().map(LimsPreFolderBean::getId).collect(Collectors.toList());
        List<LimsPreTaskSupplementBean> preTaskSupplementBeans = preTaskSupplementService.getDao().selectListByOneColumnValues(preFolderIds, "preFolderId");
        if (preTaskSupplementBeans.stream().anyMatch(e -> StringUtils.equals(e.getRequiredFlag(), "1")
                && StringUtils.isBlank(e.getFieldValue()))) {
            this.addConstraintViolation(I18nHelper.getMessage("OPERATION_BATCH.SUBBMIT.CHECK_SUPPLEMENT_NOTEMPTY"));
            return false;
        }
        return true;
    }
}
